package com.ilop.siteraudio.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ilop.siteraudio.R;
import com.ilop.siteraudio.data.bean.RecordBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2019-12-30.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class RecordListAdapter extends RecyclerView.Adapter<RecordListAdapter.ItemHolder> {

    private final Context mContext;
    private List<RecordBean> mList;
    private final ArrayList<Integer> mRecordId;
    private boolean isBoxVisibility = false;
    private final onLocalFileItemClick mCallBack;

    public RecordListAdapter(Context context, onLocalFileItemClick callBack) {
        this.mContext = context;
        this.mCallBack = callBack;
        mRecordId = new ArrayList<>();
    }

    public void setList(List<RecordBean> lists) {
        this.mList = lists;
        notifyDataSetChanged();
    }

    public void showSelectBox(boolean visibility) {
        this.isBoxVisibility = visibility;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_local_record, viewGroup, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int i) {
        itemHolder.checkBox_box.setVisibility(isBoxVisibility ? View.VISIBLE : View.GONE);
        if (!isBoxVisibility) {
            itemHolder.checkBox_box.setChecked(false);
            mRecordId.clear();
        }
        RecordBean record = mList.get(i);
        itemHolder.textView_name.setText(record.getTitle());
        itemHolder.textView_time.setText(record.getTime());
        itemHolder.itemView.setOnClickListener(view -> mCallBack.onRecordClick(record.getRecordId()));
        itemHolder.checkBox_box.setOnCheckedChangeListener((compoundButton, b) -> {
            if (isBoxVisibility) {
                if (b) {
                    mRecordId.add((int) record.getRecordId());
                } else {
                    mRecordId.remove((int) record.getRecordId());
                }
                mCallBack.onSelectReport(mRecordId);
            }
        });

        itemHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                mCallBack.onRecordLongClick(record.getPath());
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public static class ItemHolder extends RecyclerView.ViewHolder {
        @SuppressLint("NonConstantResourceId")
        @BindView(R.id.item_device_name)
        TextView textView_name;
        @SuppressLint("NonConstantResourceId")
        @BindView(R.id.item_record_time)
        TextView textView_time;
        @SuppressLint("NonConstantResourceId")
        @BindView(R.id.item_selection_box)
        CheckBox checkBox_box;

        public ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface onLocalFileItemClick{

        void onRecordClick(long recordId);

        void onRecordLongClick(String path);

        void onSelectReport(ArrayList<Integer> report);
    }
}